The PDFDocument and PDFPage classes in the Scripting app provide a simplified and powerful interface for working with PDF files, including reading, editing, and exporting content. Both synchronous and asynchronous methods are supported for optimal flexibility.
PDFPage ClassRepresents a single page within a PDF document. It offers access to text content, raw data, and related metadata.
PDFPage.fromImage(image: UIImage): PDFPage | nullCreates a new PDF page from the given image.
Parameters:
image: The image to convert into a PDF page.Returns: A PDFPage instance, or null if creation fails.
document: PDFDocument | nullThe parent PDFDocument instance this page belongs to, or null if it hasn’t been added to a document yet.
label: string | nullA user-visible label for the page, such as a page title.
numberOfCharacters: numberThe total number of text characters on the page.
string: Promise<string | null>Returns the text content of the page. May return null if the page contains only images or non-text content.
data: Promise<Data | null>Returns the raw binary representation of the page.
PDFDocument ClassRepresents an entire PDF document. This class enables reading, inspecting, editing pages, and saving the document.
PDFDocument.fromData(data: Data): PDFDocument | nullCreates a new document from raw PDF data.
Parameters:
data: A valid PDF binary buffer.Returns: A PDFDocument instance or null if the data is invalid.
PDFDocument.fromFilePath(filePath: string): PDFDocument | nullLoads a PDF document from a file path.
Parameters:
filePath: Path to a valid PDF file.Returns: A PDFDocument instance, or null if the file cannot be read.
pageCount: numberThe total number of pages in the document.
filePath: string | nullThe original file path of the PDF, or null if created in memory.
isLocked: booleanIndicates whether the document is locked and requires a password.
isEncrypted: booleanIndicates whether the document is encrypted.
documentAttributes: object | nullOptional document metadata:
data: Promise<Data | null>Asynchronously retrieves the document's binary data.
string: Promise<string | null>Asynchronously retrieves the full text content of the PDF. May return null for image-based documents.
pageAt(index: number): PDFPage | nullReturns the page at the given index.
Parameters:
index: Zero-based index of the page.Returns: A PDFPage instance or null if out of range.
indexOf(page: PDFPage): numberReturns the index of the specified page within the document.
Parameters:
page: A PDFPage object from this document.Returns: The page index, or -1 if not found.
removePageAt(index: number): voidRemoves the page at the specified index.
insertPageAt(page: PDFPage, atIndex: number): voidInserts a page at the specified index.
exchangePage(atIndex: number, withPageIndex: number): voidSwaps two pages in the document.
writeSync(toFilePath: string, options?): booleanWrites the document to a file synchronously with optional encryption and configuration.
Parameters:
toFilePath: Path to save the new PDF.
options (optional):
Returns: true if the file was saved successfully, false otherwise.
write(toFilePath: string, options?): Promise<boolean>Asynchronously writes the document to a file.
writeSync.Promise<boolean> indicating success.unlock(password: string): booleanAttempts to unlock an encrypted PDF document.
Parameters:
password: The password string.Returns: true if unlocked successfully, otherwise false.